package com.practice.one;

import java.util.ArrayList;
import java.util.Scanner;

public class TestPractice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();

        lo:while(true) {
            System.out.println("----欢迎来到学生管理系统----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查找学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择：");
            String x = sc.next();
            switch (x){
                case "1":
                    insertStudent(list);
                    break;
                case "2":
                        deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    selectStudent(list);
                    break;
                case "5":
                    System.out.println("结束");
                    break lo;//退出死循环
                    default:
                        System.out.println("您输入有误");
                        break ;
            }
        }
    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学号:");
        int number = sc.nextInt();
        int i = selectNum(list,number);
        if (i==-1){
            System.out.println("您输入学号有误");
        }else {
            System.out.println("请输入新姓名");
            String name = sc.next();
            System.out.println("请输入新年龄");
            int age = sc.nextInt();
            Student s = new Student(number,name,age);
            list.set(i,s);
            System.out.println("修改成功");
        }
    }

    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学号:");
        int number = sc.nextInt();
        int i = selectNum(list,number);
        if (i==-1){
            System.out.println("您输入数据有误");
        }else {
            list.remove(i);
            System.out.println("删除成功");
        }
    }

    public static void selectStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("没有数据");
        }
        else{
        System.out.println("学号\t姓名\t年龄\t");
        for(int i=0;i<list.size();i++) {
            Student s = list.get(i);
            System.out.println(s.getNumber() + "\t\t" + s.getName() + "\t\t" + s.getAge());
        }
        }
    }

    public static void insertStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        int number ;//定义在外面提高作用域，让最下面的也能用
        ll:while (true) {
            System.out.println("请输入学号:");
             number=sc.nextInt();
            int i = selectNum(list, number);
            if(i!=-1){
                System.out.println("学号已经存在");
            }else {
                break ll;
            }
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        Student s = new Student(number,name,age);
        list.add(s);
        System.out.println("添加成功");
    }
//    查找学号对应的索引位置
    public static int selectNum(ArrayList<Student> list,int number) {
        int index=-1;
        for (int i=0;i<list.size();i++){
            Student s = list.get(i);
            if(number==s.getNumber()) {
                index=i;
            }
        }return index;
    }
}
